let container = document.querySelector('ul')
let foot = document.querySelector('.footer1>div>b:nth-of-type(3)')
let span = document.querySelector('span')
function render(data, el) {
    console.log(data, el)
    let domStr = ''
    data.forEach(item => {
        let regExp = /(?<=imgurl=)[^'"]*/
        let result = item.productImg2.match(regExp)
        result = result[0]
        console.log(result)
        domStr += `<li productid=${item.productId}><img src="${result}" alt=""><b>${item.productName}</b>
            <b>${item.productComCount}</b><b>立即抢购</b></li>`
        el.innerHTML = domStr
    })
}
function getData(pageid = 0) {
    fetch(`http://chst.vip:1234/api/getmoneyctrl?pageid=${pageid}`)
        .then(body => body.json())
        .then(res => {
            console.log(res)
            render(res.result, container)
        })
}
function lazyLoad(fn, threshold = 200) {
    let flag = true;
    window.onscroll = function () {
        let scrollHeight = document.documentElement.scrollHeight;
        let clientHeight = document.documentElement.clientHeight;
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        let m = scrollHeight - scrollTop - clientHeight;

        if (m <= threshold) {
            if (flag) {
                flag = false;
                fn()
            }
        } else {
            flag = true;
        }
    }
}
getData()
let pageid = 1
lazyLoad(function () {
    pageid++
    if (pageid > 14) {
        return
    } else {
        getData(pageid)
    }
}, 50)

container.onclick = function (e) {
    console.log(e.target)
    if (e.target.localName == 'li') {
        let _this = e.target
        let id = _this.getAttribute('productid')
        console.log(id);
        location.href = "./../html/xiangqingye.html?productid=" + id
        id = Number(id)
    }
}

span.onclick = function () {
    location.href = "../index.html"
}
foot.onclick = function () {
    let scrollTop = document.documentElement.scrollTop
    console.log(scrollTop)
    document.documentElement.scrollTop = 0;
}